package com.nut.spp.api.admin;

import com.nut.spp.api.resp.MenuVo;
import com.nut.spp.common.Result;
import com.nut.spp.entity.SysMenu;
import com.nut.spp.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping(value = "/list")
    public Result<List<SysMenu>> list(@RequestParam(required = false) String name) {
        List<SysMenu> list = sysMenuService.list(name);
        return Result.ok(list);
    }

    @GetMapping(value = "/lazy")
    public Result<List<SysMenu>> lazy(@RequestParam(name = "pid", defaultValue = "0") Integer pid) {
        List<SysMenu> list = sysMenuService.getByPid(pid);
        return Result.ok(list);
    }

    @GetMapping(value = "/{id}")
    public Result<SysMenu> detail(@PathVariable Integer id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }

    @GetMapping("/superior")
    public Result<List<MenuVo>> getSuperior(@RequestParam Integer id) {
        List<MenuVo> list = sysMenuService.getSuperior(id, new ArrayList<>());
        List<MenuVo> tree = sysMenuService.buildTree(list);
        return Result.ok(tree);
    }

    @PostMapping
    public Result add(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @PutMapping
    public Result update(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        sysMenuService.deleteByIds(ids);
        return Result.ok();
    }

//    @PutMapping(value = "addRoleMenus")
//    public Result<?> addRoleMenus(@RequestBody SysRole sysRole) {
//        return sysRoleService.addRoleMenus(sysRole, sysRole.getMenuIds());
//    }
//
//    @PutMapping(value = "updateRoleMenus")
//    public Result<?> updateRoleMenus(@RequestBody SysRole sysRole) {
//        return sysRoleService.updateRoleMenus(sysRole.getId(), sysRole.getMenuIds());
//    }

}
